package com.jianbo.proxy.api;

import java.io.File;
import java.io.FileInputStream;

import org.noear.snack.core.utils.StringUtil;
import org.smartboot.http.server.HttpRequest;

import com.jianbo.proxy.entity.HttpResponseMiddle;
import com.jianbo.proxy.util.FileUtil;

public class FileGetApi extends Api {

    @Override
    public String apiName() {
        return "/editor/fileGet";
    }

    @Override
    public boolean execute(HttpRequest request, HttpResponseMiddle response) throws Exception {
        String filename = request.getParameter("filename");
        if(StringUtil.isEmpty(filename)) {
            response.write("filename is empty");
            return true;
        }

        File file = new File(filename);
        
        if(file.exists()) {
            byte[] bytes = FileUtil.readBytes(new FileInputStream(file));
            response.write(bytes);
        } else {
            response.write("Not Exist");
        }
        return true;
    }
    
}
